<?php
//////////////////////////////////////
// Class Registration System		//
// CPSC 462				   			//
// Webpage Class and Functions		//
// Trent Robbins & Richard Heywood  //
//////////////////////////////////////

class Website{

	function createPage($pageTitle,$css,$javascript,$html) {
		echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
			"http://www.w3.org/TR/html4/loose.dtd">
			<html>
			<head>
				<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
				<title>
					Class Registration
				</title>
				<LINK rel="shortcut icon" href="favicon.ico" type="image/x-icon">
				<style type=\'text/css\'>
					<!--
					'.file_get_contents('./css/main.css').$css.'
					-->
				</style>
				<!--[if IE 6]>
				<![endif]-->
				<script type="text/javascript" src="./js/jquery/jquery.min.js"></script>
				'.$javascript.'
			</head> 
			<body>
				<div id="content" align="center">
					<div id="hd" class="main">
						<h1>
						'.$pageTitle.'
						</h1>
					</div>
					<div id="body">
						'.$html.'
						<div id="footer" class="footer1">
							 <br /> CPSC 462 Fall 2010.<br /><br />
						</div>
					</div>
				</div>
			</body>
			</html>' ;
	}
	
	/*This function creates a connection to the MySQL database*/
	function dbConnect(){
		$mysqlUsername="f10t06";
		$mysqlPassword="HeyRob462";
		$mysqlDatabase="f10t06";
		$mysqlHost="localhost";
		mysql_connect($mysqlHost,$mysqlUsername,$mysqlPassword);
		@mysql_select_db($mysqlDatabase) or die( "Unable to select database");
	}
	
	/*This function closes the connection to the MySQL database*/
	function dbCloseConnect(){
		mysql_close();
	}
	
	/*Returns an array of all of the course subjects in the database*/
	function getSubjects(){
		$subjectList = array();
		$queryResult = mysql_query("SELECT DISTINCT `subject` FROM Course") or die("Mysql Error");
		while($rowData = mysql_fetch_assoc($queryResult)){
			array_push($subjectList,$rowData['subject']);
		}
		return $subjectList;
	}
	
	/*Returns a string with the full subject name in English when given a subject abbreviation*/
	function getSubjectName($subjAbbrv){
		$subjectNameList = array(
			'CP SC' => 'Computer Science',
			'ENGL' => 'English',
			'NURS' => 'Nursing',
			'ENGR' => 'Engineering');
		
		return $subjectNameList[$subjAbbrv];
	}
	
	/*Returns an array of arrays*/
	function getCourseInfoBySubject($subject){
		$descriptionList = array();
		$courseNumList = array();
		$hoursList = array();
		$queryResult = mysql_query("SELECT DISTINCT `description`, `number`, `hours` FROM Course WHERE `subject`='$subject'") 
							or die("Mysql Error in getCourseInfoBySubject");
		while($rowData = mysql_fetch_assoc($queryResult)){
			array_push($descriptionList,$rowData['description']);
			array_push($courseNumList,$rowData['number']);
			array_push($hoursList,$rowData['hours']);
		}
		return array($courseNumList,$descriptionList,$hoursList);
	}
	
	/*Returns and array of course info*/
	function getCourseByCode($code){
		$queryResult = mysql_query("SELECT * FROM Course WHERE `code`='$code'") 
							or die("Mysql Error in getCourseByCode");
		$rowData = mysql_fetch_assoc($queryResult);
		return $rowData;
	}
	
	/*Returns an array of arrays with Section info in them*/
	function getSections($subject, $courseNum){
		$sections = array();
		/*get the course Code by subject and number*/
		$queryResult = mysql_query("SELECT `code`, `description`, `hours` FROM Course WHERE `subject`='$subject' AND `number`='$courseNum'") or die("Mysql Error");
		$rowData = mysql_fetch_assoc($queryResult);
			$code = $rowData['code'];
			$description = $rowData['description'];
			$hours = $rowData['hours'];
		/*Get all of the sections and corresponding info for the course code*/
		$queryResult2 = mysql_query("SELECT `sectionID`, `building`, `roomnumber`, `time`, `instructor`, `textbook`, `openclosed` FROM Section WHERE `courseCode`='$code'") or die("Mysql Error");
		while($rowData2 = mysql_fetch_assoc($queryResult2)){
			$sectionTuple = array();
			$sectionTuple[0] = $code;
			$sectionTuple[1] = $subject;
			$sectionTuple[2] = $courseNum;
			$sectionTuple[3] = $description;
			$sectionTuple[4] = $hours;
			$sectionTuple[5] = $rowData2['sectionID'];
			$sectionTuple[6] = $rowData2['building'];
			$sectionTuple[7] = $rowData2['roomnumber'];
			$sectionTuple[8] = $rowData2['time'];
			$sectionTuple[9] = $rowData2['instructor'];
			$sectionTuple[10] = $rowData2['textbook'];
			$sectionTuple[11] = $rowData2['openclosed'];
			$sections[] = $sectionTuple;
		}
		
		return $sections;
	}
	
	/*Returns a string with academic standing. Takes an integer.*/
	function getStandingByYear($year){
		$standings = array(
			'1' => 'Freshman',
			'2' => 'Sophomore',
			'3' => 'Junior',
			'4' => 'Senior');
		return $standings[$year];
	}
	
	/*Returns a string with enrollment status. Takes an integer.*/
	function processEnrollment($number){
		$status='';
		if($number==0){
			$status = 'Not Enrolled';
		}
		elseif($number==1){
			$status = 'Enrolled';
		}
		return $status;
	}
	
	/*Returns an array of personal information for the given student Id*/
	function getStudentInfo($studId){
		$queryResult = mysql_query("SELECT `firstName`, `lastName`, `major`, `year`, `isEnrolled`, `hoursLimit` FROM Student WHERE `id`='$studId'") or die("Mysql Error");
		$rowData = mysql_fetch_assoc($queryResult);
		return array($rowData['firstName'],
					 $rowData['lastName'],
					 $rowData['major'],
					 $rowData['year'],
					 $rowData['isEnrolled'],
					 $rowData['hoursLimit']);
	}
	
	/*Returns an array of personal information about the specified instructor*/
	function getInstructorInfo($Id){
		$queryResult = mysql_query("SELECT `firstName`, `lastName`, `officeHours`, `building`, `roomNumber`, `webpage`, `rmpLink` FROM Instructor 
			WHERE `id`='$Id'") or die("Mysql Error in getInstructorInfo");
		$rowData = mysql_fetch_assoc($queryResult);
		
		/*Add if there are empty values, add default values instead*/
		if($rowData['officeHours']==''){
			$rowData['officeHours'] = 'By Appointment';
		}
		if($rowData['building']==''){
			$rowData['building'] = 'Not Available';
			$rowData['roomNumber'] = '';
		}
		if($rowData['webpage']==''){
			$rowData['webpage'] = 'Not Available';
		}
		else{
			$rowData['webpage']="<a href='{$rowData['webpage']}'>{$rowData['webpage']}</a>";
		}
		if($rowData['rmpLink']==''){
			$rowData['rmpLink'] = 'Not Yet Rated';
		}
		else{
			$rowData['rmpLink']="<a href='{$rowData['rmpLink']}'>{$rowData['rmpLink']}</a>";
		}
		
		/*Return array of professor info*/
		return array($rowData['firstName'],
					 $rowData['lastName'],
					 $rowData['officeHours'],
					 $rowData['building'],
					 $rowData['roomNumber'],
					 $rowData['webpage'],
					 $rowData['rmpLink']);
	}
	
	/*Returns an array of information about the specified textbook*/
	function getTextbookInfo($ISBN){
		$queryResult = mysql_query("SELECT `name`, `author`, `edition`, `amazonLink` FROM Textbook WHERE `isbn`='$ISBN'") 
			or die("Mysql Error in getTextbookInfo");
		$rowData = mysql_fetch_assoc($queryResult);
		/*Add if there are empty values, add default values instead*/
		if($rowData['amazonLink']==''){
			$rowData['amazonLink'] = 'Not Available';
		}
		else{
			$rowData['amazonLink'] = "<a href='{$rowData['amazonLink']}'>Amazon.com - {$rowData['name']}</a>";
		}	
		/*Return array of textbook info*/
		return array($rowData['name'],
					 $rowData['author'],
					 $rowData['edition'],
					 $rowData['amazonLink']);
	}
	
	/*Returns a string containing a table of all of the courses the student is registered for*/
	function getStudentCurrentCourses($studId){
		$registeredSections = array();
		$queryResult = mysql_query("SELECT DISTINCT * FROM Course, Section 
										WHERE Course.code = Section.courseCode 
											AND (Course.code, Section.sectionID) 
												IN (SELECT courseCode, sectionID FROM StudentsInSection WHERE studentID = $studId)") 
													or die("Mysql Error in getStudentCurrentCourses");
		while($rowData = mysql_fetch_assoc($queryResult)){
			$sectionInfo = array();
			$sectionInfo[0] = $rowData['code'];
			$sectionInfo[1] = $rowData['subject'];
			$sectionInfo[2] = $rowData['number'];
			$sectionInfo[3] = $rowData['description'];
			$sectionInfo[4] = $rowData['hours'];
			$sectionInfo[5] = $rowData['sectionID'];
			$sectionInfo[6] = $rowData['building'];
			$sectionInfo[7] = $rowData['roomnumber'];
			$sectionInfo[8] = $rowData['time'];
			$sectionInfo[9] = $rowData['instructor'];
			$sectionInfo[10] = $rowData['textbook'];
			$sectionInfo[11] = $rowData['openclosed'];
			$registeredSections[] = $sectionInfo;
		}
		$body = $this->makeCourseTable($registeredSections,$studId,1);
		
		return $body;
	}
	
	/*Returns a string containing a table of all of the courses the professor is teaching*/
	function getProfCourses($profId,$studId){
		$profCourses = array();
		$queryResult = mysql_query("SELECT * FROM Course, Section 
										WHERE Course.code = Section.courseCode 
											AND (Course.code, Section.sectionID) 
											IN (SELECT courseCode, sectionID FROM Section WHERE `instructor`='$profId')") 
												or die("Mysql Error in getProfCourses");
		while($rowData = mysql_fetch_assoc($queryResult)){
			$sectionInfo = array();
			$sectionInfo[0] = $rowData['code'];
			$sectionInfo[1] = $rowData['subject'];
			$sectionInfo[2] = $rowData['number'];
			$sectionInfo[3] = $rowData['description'];
			$sectionInfo[4] = $rowData['hours'];
			$sectionInfo[5] = $rowData['sectionID'];
			$sectionInfo[6] = $rowData['building'];
			$sectionInfo[7] = $rowData['roomnumber'];
			$sectionInfo[8] = $rowData['time'];
			$sectionInfo[9] = $rowData['instructor'];
			$sectionInfo[10] = $rowData['textbook'];
			$sectionInfo[11] = $rowData['openclosed'];
			$profCourses[] = $sectionInfo;
		}
		$body = $this->makeCourseTable($profCourses,$studId,2);
		return $body;
	}
	
	/*Returns a string containing a table of all of the courses the student has requested*/
	function getRequestedCourses($studId){
		$requestedSections = array();
		$queryResult = mysql_query("SELECT DISTINCT * FROM Course, Section 
										WHERE Course.code = Section.courseCode 
											AND (Course.code, Section.sectionID) 
												IN (SELECT courseCode, sectionID FROM CoursesRequested WHERE studentID = $studId)") 
													or die("Mysql Error in getRequestedCourses");
		while($rowData = mysql_fetch_assoc($queryResult)){
			$sectionInfo = array();
			$sectionInfo[0] = $rowData['code'];
			$sectionInfo[1] = $rowData['subject'];
			$sectionInfo[2] = $rowData['number'];
			$sectionInfo[3] = $rowData['description'];
			$sectionInfo[4] = $rowData['hours'];
			$sectionInfo[5] = $rowData['sectionID'];
			$sectionInfo[6] = $rowData['building'];
			$sectionInfo[7] = $rowData['roomnumber'];
			$sectionInfo[8] = $rowData['time'];
			$sectionInfo[9] = $rowData['instructor'];
			$sectionInfo[10] = $rowData['textbook'];
			$sectionInfo[11] = $rowData['openclosed'];
			$requestedSections[] = $sectionInfo;
		}
		$body = $this->makeCourseTable($requestedSections,$studId,1);
		
		return $body;
		
	}
	
	/*Returns a string with a row of the table header*/
	function makeTableHeader($type=0){
		$body = '<tr>
						<th class="courseInfo">
							Course
						</th>
						<th class="courseInfo">
							Section
						</th>
						<th class="courseInfo">
							Description
						</th>
						<th class="courseInfo">
							Hours
						</th>
						<th class="courseInfo">
							Location
						</th>
						<th class="courseInfo">
							Meeting Time
						</th>';
		if($type!=2){
			$body .=	'<th class="courseInfo">
							Instructor
						</th>';
		}
		$body .=		'<th class="courseInfo">
							Textbook
						</th>';
		$body .=		'<th class="courseInfo">
							Prereqs
						</th>';
		/*Changes the columns based on the type specified*/
		if($type==0){
			$body .=	'<th class="courseInfo">
								Status
						</th>';
		}
		else{
			$body .= 	'';
		}
		/*If this is the table of the professor's courses, then don't create an Action button*/
		if($type!=2){
			$body .=	'<th class="courseInfo">
							Action
						</th>';
		}
		else{
			$body .= 	'';
		}
					'</tr>';
		return $body;
	}
	
	/*Takes array of arrays with Section info in each one:
	  $sectionArray[0] => ( [0] => code 
							[1] = subject;
							[2] = courseNum;
							[3] = description;
							[4] = hours;
							[5] = sectionID
							[6] = building
							[7] = roomnumber
							[8] = time
							[9] = instructor
							[10] = textbook
							[11] = openclosed ) */
	function makeCourseTable($sectionDblArray,$studId,$type=0){
		$body = '';
		/*Table Header*/
		$body .= '<table class="courseInfo">';
		$body .= $this->makeTableHeader($type);
		
		/*Add a forloop here to make the rows*/
		foreach($sectionDblArray as $sectionInfo){
			/*Check for course conflicts*/
			//$err = $this->checkForConflicts($sectionInfo,$studId,);
			$body .= $this->processSectionArray($sectionInfo,$studId,$type);
		}
		$body .='</table>';
		return $body;
	}
	
	/*Returns string that contains a row with 11 td's that contain the course info*/
	function processSectionArray($sectionInfo,$studId,$type=0){
		$code = $sectionInfo[0];
		$course = "$sectionInfo[1] $sectionInfo[2]";
		$description = $sectionInfo[3];
		$hours = $sectionInfo[4];
		$section = $sectionInfo[5];
		$location = "$sectionInfo[6] $sectionInfo[7]";
		$time = $sectionInfo[8];
		$instructorID = $sectionInfo[9];
		$ISBN = $sectionInfo[10];
		$openClosed = $sectionInfo[11];
		/*Create a row with the section information*/
		$body = '<tr>';
		$body .= "	<td class='courseInfo'>
						$course
					</td>
					<td class='courseInfo'>
						$section
					</td>
					<td class='courseInfo'>
						$description
					</td>
					<td class='courseInfo'>
						$hours
					</td>
					<td class='courseInfo'>
						$location
					</td>
					<td class='courseInfo'>
						$time
					</td>";
		if($type!=2){
			$instructor = $this->getInstructorByID($instructorID);
			$body .="<td class='courseInfo'>
						<a href='./professor.php?studId=$studId&instructorId=$instructorID' target='_blank'>$instructor</a>
					</td>";
		}
		$textbook = $this->getTextbookByISBN($ISBN);
		if($textbook == ''){
			$textbook = 'Not Yet Listed';
		}
		else{
			$textbook = "<a href='./textBook.php?studId=$studId&isbn=$ISBN' target='_blank'>$textbook</a>";
		}
		$body .=	"<td class='courseInfo'>
						$textbook
					</td>";
		$body .= "<td class='courseInfo'>
					<a href='' onclick=\"window.open('./prereqs.php?studId=$studId&courseCode=$code','popup','width=500,height=350,scrollbars=no,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false\">Click for List</a>
				  </td>";
		/*type 0 means courseDirectory page, this is the only place you want to show the status*/
		if($type==0){
			$status = $this->getStatus($openClosed);
			$body .=	"<td class='courseInfo'>
							$status
						</td>";
		}
		else{
			$body .= "";
		}
		/*If type==2, this is for a professor, so don't add an Action Button*/
		if($type==0 || $type==1){
			$body .=	"<td class='courseInfo'>";
			$body .= 		$this->createActionButton($code, $section, $studId);
			$body .=	"</td>";
		}
		else{
			$body .= "";
		}
		$body .= '</tr>';
		return $body;
	}
	
	/*This function creates a button. It will be a(n) 
			1) Add Button if the course is available and the student is not registered for the course
			2) Drop Button if the student is registered for the course
			3) Request Button if the course is closed but the student is not registered for the course
			4) UnRequest Button if the course is closed and the student has requested the course*/
	function createActionButton($code, $section, $studId){
		$button = "<form action='formHandler.php' method='post' autocomplete='off'>
						<input type='hidden' name='form' value='1' />
						<input type='hidden' name='studId' value='$studId' />
						<input type='hidden' name='courseCode' value='$code' />
						<input type='hidden' name='sectionID' value='$section' />";		
		/*If the student is registered for the course, create an Drop Button*/
		if($this->studentInSection($code, $section, $studId)){
			$button .= "<input type='submit' name='btype' value='Drop' onclick='return dropCheck()' />";
		}
		/*If the student has requested the course, create a Drop Request button*/
		elseif($this->studentHasRequestedSection($code, $section, $studId)){
			$button .= "<input type='submit' name='btype' value='UnRequest' onclick='return dropRequest()'>";
		}
		/*If the student has not registered for the course or requested it, 
		  And if the section is not full, check the course Prereqs*/
		elseif($this->studentHasPrereqs($code, $section, $studId) && $this->sectionOpenClosed($code, $section)){
			/*If the student has the prereqs and the section is open, create an add button*/
			$button .= "<input type='submit' name='btype' value='Add'>";
		}
		/*If the student does not have the prereqs or if the section is full, 
		  create a Request button*/
		else{
			$button .= "<input type='submit' name='btype' value='Request'>";
		}
		$button .= "</form>";
		return $button;
	}
	
	/*Returns true if the student is registered for the course
	  Returns false if the student is not registered for the course*/
	function studentInSection($code, $section, $studId){
		$queryResult = mysql_query("SELECT `studentID` FROM StudentsInSection 
										WHERE `sectionID`='$section' AND `courseCode`='$code' AND `studentID`='$studId' ") 
											or die("Mysql Error");
		$rowData = mysql_fetch_assoc($queryResult);
		if($rowData['studentID']!=''){
			return true;
		}
		else{
			return false;
		}
	}
	
	/*Returns true if the student has requested the course
	  Returns false if the student has not requested the course*/
	function studentHasRequestedSection($code, $section, $studId){
		$queryResult = mysql_query("SELECT `studentID` FROM CoursesRequested 
										WHERE `sectionID`='$section' AND `courseCode`='$code' AND `studentID`='$studId' ") 
											or die("Mysql Error");
		$rowData = mysql_fetch_assoc($queryResult);
		if($rowData['studentID']!=''){
			return true;
		}
		else{
			return false;
		}
	}
	
	/*Returns true if the student has the required prereqs
	  Returns false if the student does not have the required prereqs*/
	function studentHasPrereqs($code, $studId){

		$coursesTaken = array();
		/*Grab all of the prereq course codes*/
		$prereqsList = $this->getPrereqs($code);
		/*If the course has no prereqs, return true*/
		if(empty($prereqsList)){
			return true;
		}
		/*If the course does have prereqs, check to see if the student has them*/
		$queryResult = mysql_query("SELECT code FROM CoursesTaken 
										WHERE code IN (SELECT prereqCourseCode FROM CoursePrereqs 
											WHERE courseCode = $code) AND studentID = {$_GET['studId']}") 
												or die("Mysql Error in StudentHasPrereqs");
		/*make a list of all of the courses that the student has taken that are prereqs for the given course*/
		while($rowData = mysql_fetch_assoc($queryResult)){
			$coursesTaken[] = $rowData['code'];
		}
		
		/*If none of the prereq course codes exist in the CoursesTaken table for the
		  specified student, then return false*/
		if(empty($coursesTaken)){
			return false;
		}
		/*check to see if all of the prereq course codes are in the list 
		 of prereqs the student has taken*/
		foreach($prereqsList as $prereq){
			if(!in_array($prereq,$coursesTaken)){
				return false;
			}
		}
		return true;
	}
	
	/*Returns an array of the prereqs for the supplied course code*/
	function getPrereqs($code){
		/*Get the course code for each prereq*/
		$prereqsList = array();
		$queryResult = mysql_query("SELECT prereqCourseCode FROM CoursePrereqs WHERE courseCode=$code") 
									or die("Mysql Error in getPrereqs");
		while($rowData = mysql_fetch_assoc($queryResult)){
			$prereqsList[] = $rowData['prereqCourseCode'];
		}
		return $prereqsList;
		
	}
	
	/*Returns an array of formatted strings of all prereqs for a given course*/
	function getPrereqInfo($courseCode){
		$prereqInfo = array();
		$queryResult = mysql_query("SELECT * from Course WHERE code IN 
										(SELECT prereqCourseCode FROM CoursePrereqs WHERE courseCode = $courseCode)")
											or die("Mysql Error in getPrereqInfo");
		while($rowData = mysql_fetch_assoc($queryResult)){
			$subjAbr = $rowData['subject'];
			$subject = $this->getSubjectName($subjAbr);
			$prereqInfo[] = "$subject {$rowData['number']} -- {$rowData['description']} -- {$rowData['hours']} Hours";
		}
		return $prereqInfo;
	}
	
	/*Returns a string containing the title of the textbook*/
	function getTextbookByISBN($ISBN){
		$queryResult = mysql_query("SELECT `name` FROM Textbook WHERE `isbn`='$ISBN'") or die("Mysql Error");
		$rowData = mysql_fetch_assoc($queryResult);
		return $rowData['name'];
	}
	
	/*Returns a string of the instructor's first and last name*/
	function getInstructorByID($Id){
		$queryResult = mysql_query("SELECT `firstName`, `lastName` FROM Instructor WHERE `id`='$Id'") or die("Mysql Error");
		$rowData = mysql_fetch_assoc($queryResult);
		$instructorName = "{$rowData['firstName']} {$rowData['lastName']}";
		return $instructorName;
	}
	
	/*Returns true if the section is open
	  returns false if the section is closed*/
	function sectionOpenClosed($courseCode,$sectionID){
		$queryResult = mysql_query("SELECT `openclosed` FROM Section WHERE `sectionID`='$sectionID' AND `courseCode`='$courseCode'") or die("Mysql Error in sectionOpenClosed");
		$rowData = mysql_fetch_assoc($queryResult);
		
		$queryResult2 = mysql_query("SELECT `seatsAvailable`, `seatLimit` FROM Section WHERE `sectionID`='$sectionID' AND `courseCode`='$courseCode'") or die("Mysql Error in sectionOpenClosed");
		$rowData2 = mysql_fetch_assoc($queryResult2);
		
		if($rowData2['seatsAvailable'] >= $rowData2['seatLimit']){
			$queryResult3 = mysql_query("UPDATE Section SET `openclosed` = 0
												WHERE `sectionID`='$sectionID' AND `courseCode`='$courseCode'")
													or die("MySQL Error 2 in update in sectionOpenClosed()");
			return false;
		}
		elseif($rowData['openclosed']==0){
			return false;
		}
		else{
			return true;
		}
	}
	
	/*Returns a string of either 'Open' or 'Closed'*/
	function getStatus($openClosed){
		if($openClosed == 0){
			$stat = "Closed";
		}
		elseif($openClosed == 1){
			$stat = "Open";
		}
		else{
			$stat = "";
		}
		return $stat;
	}
	
	/*Returns an the number of hours a student currently has requested*/
	function getRequestedHours($studId){
		$hours=0;
		$queryResult = mysql_query("SELECT hours FROM Course 
										WHERE code IN (SELECT courseCode FROM CoursesRequested
											WHERE studentID = $studId)") 
												or die("Mysql Error in getRegisteredHours");
		while($rowData = mysql_fetch_assoc($queryResult)){
			$hours += $rowData['hours'];
		}
		return $hours;
	}
	
	/*Returns an the number of hours a student is currently registerd for*/
	function getRegisteredHours($studId){
		$hours=0;
		$queryResult = mysql_query("SELECT hours FROM Course 
										WHERE code IN (SELECT courseCode FROM StudentsInSection
											WHERE studentID = $studId)") 
												or die("Mysql Error in getRegisteredHours");
		while($rowData = mysql_fetch_assoc($queryResult)){
			$hours += $rowData['hours'];
		}
		return $hours;
	}
	
	/**/
	function dropSection($code, $section, $studId){
		/*Check number of seats currently taken*/
		$queryResult1 = mysql_query("SELECT `seatsAvailable` FROM Section 
										WHERE `sectionID`='$section' AND `courseCode`='$code'")
											or die("MySQL Error 1 in delete in dropSection()");
		$rowData = mysql_fetch_assoc($queryResult1);
		/*If seatsAvailable is negative, that is an error, so set it to 0.*/
		if($rowData['seatsAvailable'] < 0){
			$queryResult2 = mysql_query("UPDATE Section SET `seatsAvailable` = 0 
											WHERE `sectionID`='$section' AND `courseCode`='$code'")
												or die("MySQL Error 2 in delete in dropSection()");
		}
		/*If seatsAvailable is positive, subtract 1 from the number of seats in the corresponding Section tuple*/
		elseif($rowData['seatsAvailable'] > 0){
			$queryResult2 = mysql_query("UPDATE Section SET `seatsAvailable` = `seatsAvailable` - 1 
											WHERE `sectionID`='$section' AND `courseCode`='$code'")
												or die("MySQL Error 3 in delete in dropSection()");
		}
		
		/*Delete given tuple from StudentsInSection*/
		$queryResult3 = mysql_query("DELETE FROM StudentsInSection 
										WHERE `sectionID`='$section' AND `courseCode`='$code' AND `studentID`='$studId'") 
											or die("MySQL Error 4 in delete in dropSection()");
	}
	
	/**/
	function unRequestSection($code, $section, $studId){
		$queryResult = mysql_query("DELETE FROM CoursesRequested 
										WHERE `sectionID`='$section' AND `courseCode`='$code' AND `studentID`='$studId'") 
											or die("MySQL Error in unRequestSection()");
	}

	/**/
	function addSection($code, $section, $studId){
		/*Check number of seats currently taken*/
		$queryResult1 = mysql_query("SELECT `seatsAvailable`, `seatLimit` FROM Section 
										WHERE `sectionID`='$section' AND `courseCode`='$code'")
											or die("MySQL Error 1 in select in addSection()");
		$rowData = mysql_fetch_assoc($queryResult1);
		/*if the section is not full*/
		if($rowData['seatsAvailable'] < $rowData['seatLimit']){
			/*Updates the number of seats in the corresponding Section tuple*/
			$queryResult2 = mysql_query("UPDATE Section SET `seatsAvailable` = `seatsAvailable` + 1 
												WHERE `sectionID`='$section' AND `courseCode`='$code'")
													or die("MySQL Error 2 in update in addSection()");
			/*Adds tuple to StudentsInSection*/
			$queryResult = mysql_query("INSERT INTO StudentsInSection (sectionID, courseCode, studentID)
											VALUES ('$section','$code','$studId')") 
												or die("MySQL Error in addSection()");
		}
		/*If the section is full, close the section. This is where we need a trigger. 
		  This is also done in sectionOpenClosed()*/
		else{
			$queryResult2 = mysql_query("UPDATE Section SET `openclosed` = 0
												WHERE `sectionID`='$section' AND `courseCode`='$code'")
													or die("MySQL Error 2 in update in addSection()");
		}
		
	}

	/**/
	function requestSection($code, $section, $studId){
		$queryResult = mysql_query("INSERT INTO CoursesRequested (sectionID, courseCode, studentID)
										VALUES ('$section','$code','$studId')") 
											or die("MySQL Error in addSection()");
	}

	/*inserts a new User into the db*/
	function addNewUser($userInfo){
		//debug statement
		/*echo "Inside func<br>";
		foreach($userInfo as $i){
			echo "$i <br>";
		}*/
		$queryResult = mysql_query("INSERT INTO Student (firstName, lastName, major, year, isEnrolled, username, password, userType, hoursLimit)
										VALUES ('{$userInfo['fname']}','{$userInfo['lname']}','{$userInfo['major']}','{$userInfo['year']}','{$userInfo['enroll']}','{$userInfo['uname']}','{$userInfo['pwd']}','Student','{$userInfo['hoursLimit']}')") 
											or die("MySQL Error in addNewUSer()");
	}
	
	/*Returns a list of all of the users*/
	function getUsers(){
		$uList = array();
		$queryResult = mysql_query("SELECT * FROM Student") or die("Mysql Error in getUsers");
		/*while($rowData = mysql_fetch_assoc($queryResult)){
			$uList
		}*/
	}
	
	function deleteUser($id){
		$queryResult = mysql_query("DELETE FROM Student WHERE `id`='$id'") or die("MySQL Error in deleteStudent()");
	}
	
	function makeUsersTable(){
		$body = "<table class='studInfo'>";
		
		$queryResult = mysql_query("SELECT * FROM Student") or die("Mysql Error in getUsers");
		while($rowData = mysql_fetch_assoc($queryResult)){
			$body .="<tr>
						<td class='studInfo'>
							{$rowData['firstName']} {$rowData['lastName']}
						</td>
						<td class='studInfo'>
							<a href='./adminMainPage.php?admId={$_GET['admId']}&userType=Admin&uname={$_GET['uname']}&func=U&act=edit&user={$rowData['id']}'>Edit</a>
						</td>
					 </tr>";
		}
		$body .= "</table>";
		return $body;
	}
	
	function getUserInfo($studId){
		$queryResult = mysql_query("SELECT * FROM Student WHERE id=$studId") or die("Mysql Error in getUserInfo");
		$rowData = mysql_fetch_assoc($queryResult);
		return $rowData;
	}
	
	function updateUser($info){
		/*$queryResult2 = mysql_query("UPDATE Student SET firstName = $info['firstName']
												WHERE `sectionID`='$section' AND `courseCode`='$code'")
													or die("MySQL Error 2 in update in addSection()");*/
	}
}
?>